package com.songqintuan.common.util;

import java.util.List;
import java.util.Map;

import javax.el.MethodExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;

public final class UIComponentUtil {
	public UIComponentUtil() {
	  }
	public static List<UIComponent> getComponentChildren(UIComponent component,Map componentMap){ 
        List<UIComponent> componentList = null; 
        addComponentToAllList(component,componentMap); 
        if(component.getChildCount() > 0){ 
            for(UIComponent ui : component.getChildren()){ 
                componentList = getComponentChildren(ui,componentMap); 
            } 
        } 
        return componentList; 
    }
	
	public static void addComponentToAllList(UIComponent component,Map<String,UIComponent> map) {
		map.put(component.getId(), component);
	}
	
	public static MethodExpression createAction(String actionExpression, Class<?> returnType) {
	    FacesContext context = SpringFacesUtil.getFacesContext();
	    Class[] classList=new Class[1];
        classList[0]=ActionEvent.class;
	    return context.getApplication().getExpressionFactory()
	        .createMethodExpression(context.getELContext(), actionExpression, returnType, new Class[0]);
	}

	public static MethodExpressionActionListener createActionListener(String actionListenerExpression) {
		FacesContext context = SpringFacesUtil.getFacesContext();
		Class[] classList=new Class[1];
        classList[0]=ActionEvent.class;
	    return new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
	        .createMethodExpression(context.getELContext(), actionListenerExpression, null, classList));
	}
}
